package cn.maihe.elg.operation.web.bank;

import cn.maihe.elg.operation.supports.bank.dto.CiticAccountDetailRespDTO;
import cn.maihe.elg.operation.supports.bank.service.CiticBankAccountDetailService;
import cn.maihe.elg.operation.schedule.CiticBankAccountDetailSchedule;
import cn.maihe.elg.operation.utils.ElgLogger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * @description: 中信银行
 */
@RestController
@RequestMapping("/api/bank/citic")
@Slf4j
public class CiticBankController {

    @Resource
    private CiticBankAccountDetailService citicBankAccountDetailService;
    
    @Resource
    private CiticBankAccountDetailSchedule citicBankAccountDetailSchedule;

    /**
     * 查询当日流水
     */
    @GetMapping("/details")
    public Map<String, Object> queryTodayFullTransactions() {
        Map<String, Object> result = new HashMap<>();
        try {
            String today = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd"));
            CiticAccountDetailRespDTO response = citicBankAccountDetailService.queryFullTransactionsByDate(today, today);
            result.put("success", true);
            result.put("data", response);
            result.put("message", "查询成功");
            
            ElgLogger.info(log, l -> l.info("查询中信银行当日流水成功，共{}条记录", 
                response != null && response.getReturnRecords() != null ? response.getReturnRecords() : 0));
                
        } catch (Exception e) {
            ElgLogger.error(log, l -> l.error("查询中信银行当日流水异常: {}", e.getMessage(), e));
            
            result.put("success", false);
            result.put("message", "查询失败: " + e.getMessage());
        }
        
        return result;
    }

    /**
     * 查询指定日期范围的账户明细
     */
    @PostMapping("/query")
    public Map<String, Object> queryAccountDetails(@RequestBody Map<String, Object> params) {
        Map<String, Object> result = new HashMap<>();
        try {
            String startDate = (String) params.get("startDate");
            String endDate = (String) params.get("endDate");
            CiticAccountDetailRespDTO response = citicBankAccountDetailService.queryFullTransactionsByDate(startDate, endDate);
            result.put("success", true);
            result.put("data", response);
            result.put("message", "查询成功");
            
            ElgLogger.info(log, l -> l.info("查询中信银行账户明细成功，共{}条记录", 
                response != null && response.getReturnRecords() != null ? response.getReturnRecords() : 0));
                
        } catch (Exception e) {
            ElgLogger.error(log, l -> l.error("查询中信银行账户明细异常: {}", e.getMessage(), e));
            
            result.put("success", false);
            result.put("message", "查询失败: " + e.getMessage());
        }
        
        return result;
    }

    /**
     * 手动触发账户明细查询
     */
    @PostMapping("/details/query")
    public Map<String, Object> manualQueryAccountDetails() {
        Map<String, Object> result = new HashMap<>();
        
        try {
            ElgLogger.info(log, l -> l.info("手动触发中信银行账户明细查询"));
            
            citicBankAccountDetailSchedule.manualQueryAccountDetails();
            
            result.put("success", true);
            result.put("message", "手动查询触发成功");
            
            ElgLogger.info(log, l -> l.info("手动触发中信银行账户明细查询成功"));
                
        } catch (Exception e) {
            ElgLogger.error(log, l -> l.error("手动触发中信银行账户明细查询异常: {}", e.getMessage(), e));
            
            result.put("success", false);
            result.put("message", "手动查询失败: " + e.getMessage());
        }
        
        return result;
    }

    /**
     * 获取交易状态描述
     */
    @GetMapping("/transaction/{status}")
    public Map<String, Object> getTransactionStatusDesc(@PathVariable String status) {
        Map<String, Object> result = new HashMap<>();
        
        try {
            String desc = citicBankAccountDetailService.getTransactionStatusDesc(status);
            
            result.put("success", true);
            result.put("status", status);
            result.put("description", desc);
            result.put("message", "获取成功");
                
        } catch (Exception e) {
            ElgLogger.error(log, l -> l.error("获取交易状态描述异常: {}", e.getMessage(), e));
            
            result.put("success", false);
            result.put("message", "获取失败: " + e.getMessage());
        }
        
        return result;
    }

    /**
     * 启用Mock模式（仅用于测试）
     */
    @PostMapping("/mock/enable")
    public Map<String, Object> enableMockMode() {
        Map<String, Object> result = new HashMap<>();
        
        try {
            citicBankAccountDetailService.enableMockMode();
            
            result.put("success", true);
            result.put("message", "Mock模式已启用");
            result.put("mockMode", true);
            
            ElgLogger.info(log, l -> l.info("Mock模式已启用"));
                
        } catch (Exception e) {
            ElgLogger.error(log, l -> l.error("启用Mock模式异常: {}", e.getMessage(), e));
            
            result.put("success", false);
            result.put("message", "启用Mock模式失败: " + e.getMessage());
        }
        
        return result;
    }

    /**
     * 禁用Mock模式（仅用于测试）
     */
    @PostMapping("/mock/disable")
    public Map<String, Object> disableMockMode() {
        Map<String, Object> result = new HashMap<>();
        
        try {
            citicBankAccountDetailService.disableMockMode();
            
            result.put("success", true);
            result.put("message", "Mock模式已禁用");
            result.put("mockMode", false);
            
            ElgLogger.info(log, l -> l.info("Mock模式已禁用"));
                
        } catch (Exception e) {
            ElgLogger.error(log, l -> l.error("禁用Mock模式异常: {}", e.getMessage(), e));
            
            result.put("success", false);
            result.put("message", "禁用Mock模式失败: " + e.getMessage());
        }
        
        return result;
    }

    /**
     * 查询Mock模式状态（仅用于测试）
     */
    @GetMapping("/mock/status")
    public Map<String, Object> getMockModeStatus() {
        Map<String, Object> result = new HashMap<>();
        
        try {
            boolean mockMode = citicBankAccountDetailService.isMockMode();
            
            result.put("success", true);
            result.put("mockMode", mockMode);
            result.put("message", "查询成功");
                
        } catch (Exception e) {
            ElgLogger.error(log, l -> l.error("查询Mock模式状态异常: {}", e.getMessage(), e));
            
            result.put("success", false);
            result.put("message", "查询失败: " + e.getMessage());
        }
        
        return result;
    }

} 